< previous page page_241 next page >

Page 241
0241-01.jpg
Figure 8.4
Program Stocks2 uses PaintPicture to make curves
scroll across the drawing area.
Program Stocks2, shown in Figure 8.4, uses a slightly different approach. Instead of clearing the entire drawing area, this program uses the PaintPicture method to copy the current image shifted slightly to the left. It then draws its newest data on the right. This makes the stock value curves scroll across the form from right to left.
Save and Restore
Another strategy for animating moving objects is to save an image of the part of the background beneath each object. To draw the next frame, use the saved images to redraw the background. Under some circumstances, saving and restoring the parts of the image that change is faster than using Cls to erase the whole image. After restoring the original background, move the images and draw them in their new locations.
Example program Bounce2 uses the following code to animate bouncing balls. Before it draws the balls for a frame, it uses PaintPicture to save an image of the area under each ball in the picHidden picture box array. It then draws the balls and waits until it should display the next frame. At that time, the program restores the saved areas before continuing with the next frame.
e10a6a0a59f035e870d90cca05b85fce.gif
' Play the animation.
Private Sub PlayImages(ByVal ms_per_frame As Long)
Dim ball As Integer
Dim next_time As Long

    ' Get the current time.
    next_time = GetTickCount()

    ' Start the animation.
    Do While Playing
        NumPlayed = NumPlayed + 1

 
< previous page page_241 next page >